package model.item;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import model.effect.ConstantChangeStatEffect;
import model.entity.stats.Stat;
import model.entity.stats.StatSet;
import model.entity.stats.StatType;
import model.map.Direction;
import model.map.HexLocationProjection;
import model.skill.ProjectileLibrary;
import model.skill.SelfProjectile;
import utilities.*;

public class ItemLibrary implements Archiveable
{
	// why would these be static, if we aren't making the instance static?
	private static Map<String, EquippableItem> equippableItemMap;
	private static Map<String, TakeableItem> takeableItemMap;
	private static Map<String, OneShotItem> oneShotItemMap;
	
	public ItemLibrary()
	{
		equippableItemMap = new HashMap<String, EquippableItem>();
		takeableItemMap = new HashMap<String, TakeableItem>();
		oneShotItemMap = new HashMap<String, OneShotItem>();
		initLibrary();
	}
	
	public static void initLibrary()
	{
		//String name, Projectile projectile, String equipmentType, EquipmentSlot equipmentSlot, StatSet stats, int price)
		
		StatSet Spear2Hstats = new StatSet();
		Spear2Hstats.addStat(StatType.OFFENSIVERATING, new Stat(10, StatType.STRENGTH));
		Spear2Hstats.addStat(StatType.OFFENSIVERATING, new Stat(2, StatType.AGILITY));
		EquippableItem Spear2H = new EquippableItem("Spear2H", null, EquipmentType.TWOHANDED, EquipmentSlot.RIGHT_HAND, Spear2Hstats , 100);
		
		StatSet Axe1Hstats = new StatSet();
		Axe1Hstats.addStat(StatType.OFFENSIVERATING, new Stat(20, StatType.STRENGTH));
		EquippableItem Axe1H = new EquippableItem("Axe1H", null, EquipmentType.ONEHANDED, EquipmentSlot.RIGHT_HAND, Axe1Hstats , 100);
		
		StatSet Katana2Hstats = new StatSet();
		Katana2Hstats.addStat(StatType.OFFENSIVERATING, new Stat(25, StatType.STRENGTH));
		Katana2Hstats.addStat(StatType.OFFENSIVERATING, new Stat(5, StatType.AGILITY));
		EquippableItem Katana2H = new EquippableItem("Katana2H", null, EquipmentType.TWOHANDED, EquipmentSlot.RIGHT_HAND, Katana2Hstats , 500);
		
		StatSet SummonerStaffstats = new StatSet();
		SummonerStaffstats.addStat(StatType.OFFENSIVERATING, new Stat(10, StatType.INTELLECT));
		SummonerStaffstats.addStat(StatType.OFFENSIVERATING, new Stat(3, StatType.STRENGTH));
		EquippableItem SummonerStaff = new EquippableItem("SummonerStaff", null, EquipmentType.ONEHANDED, EquipmentSlot.RIGHT_HAND, SummonerStaffstats, 100);
		
		StatSet ThunderStaffstats = new StatSet();
		ThunderStaffstats.addStat(StatType.OFFENSIVERATING, new Stat(20, StatType.INTELLECT));
		ThunderStaffstats.addStat(StatType.OFFENSIVERATING, new Stat(10, StatType.STRENGTH));
		EquippableItem ThunderStaff = new EquippableItem("ThunderStaff", null, EquipmentType.ONEHANDED, EquipmentSlot.RIGHT_HAND, ThunderStaffstats, 500);
		
		StatSet FireStaffstats = new StatSet();
		FireStaffstats.addStat(StatType.OFFENSIVERATING, new Stat(50, StatType.INTELLECT));
		FireStaffstats.addStat(StatType.OFFENSIVERATING, new Stat(15, StatType.STRENGTH));
		EquippableItem FireStaff = new EquippableItem("FireStaff", null, EquipmentType.ONEHANDED, EquipmentSlot.RIGHT_HAND, FireStaffstats, 2000);
		
		StatSet Broadsword1Hstats = new StatSet();
		Axe1Hstats.addStat(StatType.OFFENSIVERATING, new Stat(20, StatType.STRENGTH));
		EquippableItem Broadsword1H = new EquippableItem("Broadsword1H", null, EquipmentType.ONEHANDED, EquipmentSlot.RIGHT_HAND, Broadsword1Hstats , 100);

		StatSet Longbowstats = new StatSet();
		Longbowstats.addStat(StatType.OFFENSIVERATING, new Stat(10, StatType.STRENGTH));
		EquippableItem LongbowRanged = new EquippableItem("LongbowRanged", null, EquipmentType.ONEHANDED, EquipmentSlot.RIGHT_HAND, Longbowstats , 100);
		
		equippableItemMap.put("Axe1H", Axe1H);
		equippableItemMap.put("Katana2H", Katana2H);
		equippableItemMap.put("Spear2H", Spear2H);
		equippableItemMap.put("SummonerStaff", SummonerStaff);
		equippableItemMap.put("ThunderStaff", ThunderStaff);
		equippableItemMap.put("FireStaff", FireStaff);
		equippableItemMap.put("Broadsword1H", Broadsword1H);
		equippableItemMap.put("LongbowRanged", LongbowRanged);
		
		
		OneShotItem BeerOneShot = new OneShotItem("BeerOneShot", ProjectileLibrary.getInstance().getSelfProjectile("selfProjectileBeerEffect"));
		oneShotItemMap.put("BeerOneShot", BeerOneShot);
		
		TakeableItem slimePotion = new TakeableItem("slimePotion", ProjectileLibrary.getInstance().getSelfProjectile("selfProjectileBeerEffect"), 100);
		takeableItemMap.put("slimePotion", slimePotion);
	}
	
	public static EquippableItem getEquippableItem(String name)
	{
		return equippableItemMap.get(name);
	}
	
	public static TakeableItem getTakeableItem(String name)
	{
		return takeableItemMap.get(name);
	}
	
	public static OneShotItem getOneShotItem(String name)
	{
		return oneShotItemMap.get(name);
	}
	
	public Archive save() {
		Archive archive = ArchiveFactory.createArchive();
		
		List<String> equippableItemMapClassList = new ArrayList<String>();
		List<String> equippableItemMapKeys = new ArrayList<String>(equippableItemMap.keySet());
		List<EquippableItem> equippableItemMapValues = new ArrayList<EquippableItem>();
		for (int i = 0; i < equippableItemMapKeys.size(); i++) {
			String key = equippableItemMapKeys.get(i);
			EquippableItem item = equippableItemMap.get(key);
			equippableItemMapClassList.add(item.getClass().getName());
			equippableItemMapValues.add(item);
		}
		archive.putAttributeList("equippableItemMapClassList", equippableItemMapClassList);
		archive.putAttributeList("equippableItemMapKeys", equippableItemMapKeys);
		archive.putManyRelationship("equippableItemMapValues", equippableItemMapValues);
		
		List<String> oneShotItemMapClassList = new ArrayList<String>();
		List<String> oneShotItemMapKeys = new ArrayList<String>(oneShotItemMap.keySet());
		List<OneShotItem> oneShotItemMapValues = new ArrayList<OneShotItem>();
		for (int i = 0; i < oneShotItemMapKeys.size(); i++) {
			String key = oneShotItemMapKeys.get(i);
			OneShotItem item = oneShotItemMap.get(key);
			oneShotItemMapClassList.add(item.getClass().getName());
			oneShotItemMapValues.add(item);
		}
		archive.putAttributeList("oneShotItemMapClassList", oneShotItemMapClassList);
		archive.putAttributeList("vKeys", oneShotItemMapKeys);
		archive.putManyRelationship("oneShotItemMapValues", oneShotItemMapValues);
		
		List<String> takeableItemMapClassList = new ArrayList<String>();
		List<String> takeableItemMapKeys = new ArrayList<String>(takeableItemMap.keySet());
		List<TakeableItem> takeableItemMapValues = new ArrayList<TakeableItem>();
		for (int i = 0; i < takeableItemMapKeys.size(); i++) {
			String key = takeableItemMapKeys.get(i);
			TakeableItem item = takeableItemMap.get(key);
			takeableItemMapClassList.add(item.getClass().getName());
			takeableItemMapValues.add(item);
		}
		archive.putAttributeList("takeableItemMapClassList", takeableItemMapClassList);
		archive.putAttributeList("takeableItemMapKeys", takeableItemMapKeys);
		archive.putManyRelationship("takeableItemMapValues", takeableItemMapValues);
		
		
		return archive;
	}
	
	public void load(Archive archive) {
		List<String> equippableItemMapClassList = archive.getAttributeList("equippableItemMapClassList");
		List<String> equippableItemMapKeys = archive.getAttributeList("equippableItemMapKeys");
		List<Archive> equippableItemMapValues = archive.getManyRelationship("equippableItemMapValues");
		equippableItemMap = new HashMap<String, EquippableItem>();
		for (int i = 0; i < equippableItemMapClassList.size(); i++) {
			String key = equippableItemMapKeys.get(i);
			EquippableItem item = (EquippableItem)ModelFactory.getInstance().createInstanceWithClassName(equippableItemMapClassList.get(i));
			item.load(equippableItemMapValues.get(i));
			equippableItemMap.put(key, item);
		}
		
		List<String> oneShotItemMapClassList = archive.getAttributeList("oneShotItemMapClassList");
		List<String> oneShotItemMapKeys = archive.getAttributeList("oneShotItemMapKeys");
		List<Archive> oneShotItemMapValues = archive.getManyRelationship("oneShotItemMapValues");
		oneShotItemMap = new HashMap<String, OneShotItem>();
		for (int i = 0; i < oneShotItemMapClassList.size(); i++) {
			String key = oneShotItemMapKeys.get(i);
			OneShotItem item = (OneShotItem)ModelFactory.getInstance().createInstanceWithClassName(oneShotItemMapClassList.get(i));
			item.load(oneShotItemMapValues.get(i));
			oneShotItemMap.put(key, item);
		}
		
		List<String> takeableItemMapClassList = archive.getAttributeList("takeableItemMapClassList");
		List<String> takeableItemMapKeys = archive.getAttributeList("takeableItemMapKeys");
		List<Archive> takeableItemMapValues = archive.getManyRelationship("takeableItemMapValues");
		takeableItemMap = new HashMap<String, TakeableItem>();
		for (int i = 0; i < takeableItemMapClassList.size(); i++) {
			String key = takeableItemMapKeys.get(i);
			TakeableItem item = (TakeableItem)ModelFactory.getInstance().createInstanceWithClassName(takeableItemMapClassList.get(i));
			item.load(takeableItemMapValues.get(i));
			takeableItemMap.put(key, item);
		}
	}
	
}
